#!/usr/bin/env node

const http=require('http'),
      url=require('url'),
      log=console.log;

let addr=process.argv[2] || 'http://www.sian.com/';

function opt(addr){
  let options=url.parse(addr);
  options.headers={
    'User-Agent':'06-redirection.js'
  }
  return options;
}

function get(options){
  
  http.get(options,(res)=>{
    log(`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`);
    log(res.headers);
    log('');

    if(res.statusCode<400 && res.statusCode>=300){
      get(opt(res.header.location));
    }else{
      res.pipe(process.stdout);
    }
  });

}

get(opt(addr));

